package com.jiuyin.dianjing.ui.fragment.community;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.ui.activity.community.ReportActivity;
import com.jiuyin.dianjing.ui.fragment.main.FragmentFive;
import com.jiuyin.dianjing.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.enums.ApiEnum.ADD_COLLECT;
import static com.jiuyin.dianjing.api.enums.ApiEnum.DELETE_TOPIC_BY_ID;


public class BottomSheetFragment extends BottomSheetDialogFragment {
    public static final String KEY_ITEM_ID = "item_id";
    public static final String KEY_SELF = "key_self";
    public static final String KEY_TARGET_ID = "target_id";
    private Unbinder mUnBinder;
    private String topicId;//帖子Id
    private ProgressDialog mDialog;
    /**
     * 标记通知那个类
     */
    private String mTargetId;
    /**
     * 是否是自己的帖子
     */
    private boolean isSelf;

    public static BottomSheetFragment newInstance(String topicId, boolean isSelf, String targetId) {
        BottomSheetFragment fragment = new BottomSheetFragment();
        Bundle args = new Bundle();
        args.putString(KEY_ITEM_ID, topicId);
        args.putString(KEY_TARGET_ID, targetId);
        args.putBoolean(KEY_SELF, isSelf);
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (getContext() == null) {
            return super.onCreateDialog(savedInstanceState);
        }
        return new BottomSheetDialog(getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (getArguments() != null) {
            topicId = getArguments().getString(KEY_ITEM_ID);
            isSelf = getArguments().getBoolean(KEY_SELF);
            mTargetId = getArguments().getString(KEY_TARGET_ID);
        }
        View view = inflater.inflate(R.layout.fragment_bottom_sheet_list_dialog, container, false);
        View deleteView = view.findViewById(R.id.ll_delete);
        if (isSelf) {
            deleteView.setVisibility(View.VISIBLE);
        }
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @OnClick({R.id.tv_favorite, R.id.tv_report, R.id.tv_cancel, R.id.tv_delete})
    public void onViewClicked(View view) {
        dismiss();
        switch (view.getId()) {
            case R.id.tv_favorite:
                collectTopic();
                break;
            case R.id.tv_report:
                Intent intent = new Intent(getActivity(), ReportActivity.class);
                intent.putExtra("topicId", topicId);
                startActivity(intent);
                break;
            case R.id.tv_cancel:
                break;
            case R.id.tv_delete:
                deleteTopic();
                break;
        }
    }

    public void showLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            return;
        }
        mDialog = new ProgressDialog(getContext());
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mDialog.setMessage(getString(R.string.progress_tips_def));
        mDialog.show();
    }

    public void dismissLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        mUnBinder.unbind();
        super.onDestroyView();
    }

    /**
     * 删除帖子
     */
    private void deleteTopic() {
        HashMap hashMap = new HashMap();
        hashMap.put(KEY_TOKEN, HelperApplication.getToken());
        hashMap.put("topic_id", topicId);
        JSONObject data = new JSONObject(hashMap);
        ProgressDialogCallback progress = new ProgressDialogCallback() {
            @Override
            public void showProgressDialog() {
                showLoading();
            }

            @Override
            public void dismissProgressDialog() {
                dismissLoading();
            }
        };
        ApiResultCallback callback = new DefaultApiResult() {
            @Override
            public void onSuccess(JsonObject data) {
                ToastUtil.showShort("删除成功");
                EventBus.getDefault().post(mTargetId);
            }
        };
        ServerApi.post(DELETE_TOPIC_BY_ID, data, null, progress, null, callback);
    }

    /**
     * 收藏帖子
     */
    private void collectTopic() {
        HashMap hashMap = new HashMap();
        hashMap.put(KEY_TOKEN, HelperApplication.getToken());
        hashMap.put("target_id", topicId);
        hashMap.put(KEY_TYPE, "1");//1 收藏 0 取消收藏
        hashMap.put("model", "1");//被收藏类型 0 资讯 1 帖子
        JSONObject data = new JSONObject(hashMap);
        ProgressDialogCallback progress = new ProgressDialogCallback() {
            @Override
            public void showProgressDialog() {
                showLoading();
            }

            @Override
            public void dismissProgressDialog() {
                dismissLoading();
            }
        };
        ApiResultCallback callback = new DefaultApiResult() {
            @Override
            public void onSuccess(JsonObject data) {
                ToastUtil.showShort("收藏成功");
                EventBus.getDefault().post(new RefreshEvent(FragmentFive.class.getSimpleName()));
            }
        };
        ServerApi.post(ADD_COLLECT, data, null, progress, null, callback);
    }
}
